Skip to content

Refactor FXIOS-15851 WindowManager tabManager API should return an optional value#33928

Merged
mattreaganmozilla merged 5 commits into
mozilla-mobile:mainfrom
mattreaganmozilla:mr/15851
May 26, 2026
Merged

Refactor FXIOS-15851 WindowManager tabManager API should return an optional value#33928
mattreaganmozilla merged 5 commits into
mozilla-mobile:mainfrom
mattreaganmozilla:mr/15851

Conversation

@mattreaganmozilla

Copy link
Copy Markdown
Collaborator

📜 Tickets

Jira ticket

💡 Description

Refactors the tabManager API in our WindowManager adopters to return an optional value. I've kept the Sentry logging in that func in place, so that unexpected or invalid UUIDs should still be visible to us.

Because we're still logging in that func, in most cases early returns are still reasonable and are preferable to us doing something like returning some other window's tabManager (which is very unsafe).

📝 Checklist

  • I filled in the ticket numbers and a description of my work
  • I updated the PR name to follow our PR naming guidelines
  • I ensured unit tests pass and wrote tests for new code
  • If working on UI, I checked and implemented accessibility (Dynamic Text and VoiceOver)
  • If adding telemetry, I read the data stewardship requirements and will request a data review
  • If adding or modifying strings, I read the guidelines and will request a string review from l10n
  • If needed, I updated documentation and added comments to complex code

@mattreaganmozilla mattreaganmozilla requested a review from a team as a code owner May 20, 2026 21:03
@mattreaganmozilla

Copy link
Copy Markdown
Collaborator Author

+cc @lmarceau Since this is ultimately more of a Tab change than an actual WindowManager change.

@mobiletest-ci-bot

mobiletest-ci-bot commented May 20, 2026

Copy link
Copy Markdown
Messages
📖 Project coverage: 42.46%

💪 Quality guardian

3 tests files modified. You're a champion of test coverage! 🚀

🥇 Perfect PR size

Smaller PRs are easier to review. Thanks for making life easy for reviewers! ✨

🙌 Friday high-five

Thanks for pushing us across the finish line this week! 🙌

💬 Description craftsman

Great PR description! Reviewers salute you 🫡

✅ New file code coverage

No new file detected so code coverage gate wasn't ran.

✅ Existing file code coverage

All modified files meet their thresholds.

Client.app: Coverage: 40.96

File Coverage
TabManagerMiddleware.swift 40.19% ⚠️
WindowManager+DebugUtilities.swift 0.0% ⚠️
SceneDelegate.swift 33.78% ⚠️
StartAtHomeMiddleware.swift 90.0%
NativeErrorPageMiddleware.swift 0.0% ⚠️
TabErrorTelemetryHelper.swift 46.02% ⚠️
TranslationsMiddleware.swift 89.13%
AppDelegate.swift 0.0% ⚠️
ToolbarMiddleware.swift 93.81%
SummarizeMiddleware.swift 97.71%
TranslationsService.swift 37.82% ⚠️
WindowManager.swift 86.74%

Generated by 🚫 Danger Swift against 35f4002


/// Convenience. Provides opportunity for safety checks or window validation.
@MainActor
func windowExists(uuid: WindowUUID) -> Bool

@mattreaganmozilla mattreaganmozilla May 20, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed windowExists() because I think it was a code smell. Generally callers should not need to directly check this.

defaults.set(tabCounts, forKey: entryPoint.defaultsKey)
}

/// It's possible for this telemetry helper to be called during onboarding flow before

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That isn't a problem anymore (the comment) ?

@mattreaganmozilla mattreaganmozilla May 21, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tabManagerAvailable func was added as a safety check, but with these refactors that safety check is now baked into the tabManager(for:) func, so each call site is forced to handle the potential nil value. So with this function gone this comment doesn't really have a great place to live now, since we're checking the optional value separately in each call site. We could maybe add the comment to the top of the file if you think it's still worth calling out?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can do that yeah, I'll let you decide, you added that comment in the first place so you have more context than me! If you think it's useful to keep then lets move it, if not then 👋

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this was a good call-out thank you; I've moved the comment to the top of the file.

@lmarceau lmarceau self-requested a review May 21, 2026 20:43
@mattreaganmozilla mattreaganmozilla removed the request for review from yoanarios May 22, 2026 18:13

@yoanarios yoanarios left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good just a small nit and unit test request

@MainActor
private func startAtHomeCheck(windowUUID: WindowUUID) -> Bool {
let tabManager = windowManager.tabManager(for: windowUUID)
guard let tabManager = windowManager.tabManager(for: windowUUID) else { return false}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
guard let tabManager = windowManager.tabManager(for: windowUUID) else { return false}
guard let tabManager = windowManager.tabManager(for: windowUUID) else { return false }

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, fixed

wrappedManager.newBrowserWindowConfigured(windowInfo, uuid: uuid)
}

func tabManager(for windowUUID: WindowUUID) -> TabManager {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code wise changes seems right my only ask it will be to add support in the mock to return nil and add at least a test in TabManagerMiddlewareTests that tests the nil value instead of force unwrap to cover that case

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be confused but isn't that a superfluous test? I don't think that actually exercises any real code in the client, right? LMK if I'm misunderstanding tho, I'm happy to update the unit tests.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, I agree we don’t want to add a test that only validates the mock. My concern is more about the new API contract: tabManager(for:) now returns an optional, and TabManagerMiddleware is one of the consumers that needs to behave safely when that value is nil.

Right now the production implementation explicitly returns nil for invalid/missing windows instead of falling back to an unsafe tab manager, so I think it’s worth having at least one unit test that exercises a real consumer path with a nil TabManager. That would protect us from future regressions where someone forces unwrapping or assumes the tab manager always exists again.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed up unit tests in 35f4002 but LMK if you had something else specific in mind.

@yoanarios yoanarios left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

@mattreaganmozilla mattreaganmozilla merged commit cb8b14a into mozilla-mobile:main May 26, 2026
9 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🚀 PR merged to main, targeting version: 151.3

skhamis pushed a commit to skhamis/firefox-ios that referenced this pull request May 28, 2026
…tional value (mozilla-mobile#33928)

* [FXIOS-15851] Refactor tabManager(for:) to return an optional value

* Unit test updates

* [FXIOS-15851] Comments

* Spacing

* [FXIOS-15851] Add unit tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants